<?php
namespace plugin\entity_manager\src\collection;

/**
 * The MIT License
 *
 * Copyright (c) <year> <copyright holders>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @author Damiano Ciarla
 * @package plugin
 * @subpackage entity_manager
 */
class JoinCollectionData
{
	/**
	 * Lista delle proprietà
	 *
	 * @var array
	 */
	private $_data = array();
	
	public function __set($name, $value)
	{
		$this->_data[$name] = $value;
	}
	
	public function __get($name)
	{
		if(isset($this->_data[$name]))
		{
			return $this->_data[$name];
		}
	}
	
	/**
	 * Metodo che in base al nome della funzione che viene impostata su questa classe
	 * restituisce il relativo oggetto
	 *
	 * @param string $name Nome della funzione
	 * 
	 * @return \plugin\entity_manager\src\AbstractEntity
	 */
	public function __call($name, $arguments)
	{
		$tableName = strtolower(implode("_", array_slice(explode(" ", preg_replace("/(([a-z])([A-Z])|([A-Z][a-z]))/", "\\2\\4 \\3\\5", $name)), 1)));
		
		$className = "\\entity_manager\\model\\" . substr($name, 3);
		$class = new $className();
		
		$fieldsName = array_flip(array_keys(get_object_vars($class)));
		
		array_walk($fieldsName, array($this, "_renameVar"), $tableName);
		
		if(count(array_intersect_key($this->_data, array_flip(array_values($fieldsName)))))
		{
			foreach($fieldsName as $field => $tableFieldName)
			{
				if(array_key_exists($tableFieldName, $this->_data))
				{
					$class->$field = $this->_data[$tableFieldName];
				}
			}
		}
		else
		{
			throw new \plugin\entity_manager\src\exception\EntityManagerException($tableName, \plugin\entity_manager\src\exception\EntityManagerException::TABLE_NOT_FOUND_INTO_JOIN_COLLECTION_DATA);
		}

		return $class;
	}
	
	/**
	 * Metodo che setta il nuovo valore dell'array delle variabili
	 *
	 * @param string $val Valore
	 * @param string $index Indice
	 * @param string $tableName Nome della tabella
	 */
	private function _renameVar(&$val, $index, $tableName)
	{
		$val = $tableName . "_" . $index;
	}
}

?>
